package com.phpxiu.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.phpxiu.app.api.OnDialogClickListener;
import com.phpxiu.app.kkylive.R;

/**
 * Created by 吴金洲 on 2016/8/11
 */
public class NotificationOptionDlg extends Dialog implements View.OnClickListener {
    private TextView titleView, descView;
    private Button ok,cancel;
    private OnDialogClickListener onDialogClickListener;
    public NotificationOptionDlg(Context context) {
        super(context, R.style.interactiveDialog);
        setContentView(R.layout.dialog);
    }

    @Override
    public void setContentView(int layoutResId) {
        if(layoutResId!=0) {
            super.setContentView(layoutResId);
            titleView = (TextView) findViewById(R.id.dialog_title);
            descView = (TextView) findViewById(R.id.dialog_description);
            cancel = (Button) findViewById(R.id.dialog_cancel);
            cancel.setOnClickListener(this);
            ok = (Button) findViewById(R.id.dialog_ok);
            ok.setOnClickListener(this);
        }
    }


    /**
     * 设置对话框标题
     * @param title 对话框标题
     */
    public NotificationOptionDlg setTitle(String title) {
        if (titleView != null) {
            titleView.setText(title);
        }
        return this;
    }

    /**
     * 设置对话框提示信息
     * @param description 对话框提示信息
     */
    public NotificationOptionDlg setDescription(String description) {
        if (descView != null) {
            descView.setText(description);
        }
        return this;
    }

    public void setOnDialogClickListener(OnDialogClickListener l){
        onDialogClickListener=l;
    }

    @Override
    public void onClick(View v) {
        if(onDialogClickListener!=null){
            if(v.getId()== R.id.dialog_ok) {
                onDialogClickListener.onOk();
            }else{
                onDialogClickListener.onCancel();
            }
        }
        dismiss();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        super.onKeyDown(keyCode,event);
        //返回键盘事件
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(getOwnerActivity()!=null){
                getOwnerActivity().onBackPressed();
            }
            return false;
        }else{
            return super.onKeyDown(keyCode,event);
        }
    }
}
